package com.proyecto.track.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.proyecto.track.bo.Period;
import com.proyecto.track.bo.Project;
import com.proyecto.track.bo.Task;
import com.proyecto.track.bo.TaskStatus;
import com.proyecto.track.bo.UserPerson;
import com.proyecto.track.service.ProjectService;
import com.proyecto.track.service.TaskService;
import com.proyecto.track.service.UserPersonService;

public class SearchAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	
	private Long idProject;
	@Autowired
	private ProjectService projectService;
	@Autowired
	private UserPersonService userPersonService;
	@Autowired
	private TaskService taskService;
	private UserPerson userPerson;
	private Period period;
	private TaskStatus taskStatus;
	private Project project;
	private List<TaskStatus> taskStatus2 = new ArrayList<TaskStatus>();
	private List<UserPerson> userPersons = new ArrayList<UserPerson>();
	private List<Project> projects = new ArrayList<Project>();
	private List<Task> tasks = new ArrayList<Task>();
	
	public String loadLists(){
		this.userPersons = userPersonService.searchUser();
		this.projects = projectService.searchProjects();
		this.taskStatus2 = taskService.listTaskStatus();
		return SUCCESS;
	}
	
	public String searchTask(){
		System.out.println("PASA POR ACA");
		
		Map<String,Object> session = ActionContext.getContext().getSession();
		Long accountId = (Long) session.get("accountId");
		this.project = projectService.searchProjects(idProject);
		UserPerson usuario = userPersonService.searchUserByAccount(accountId);
		
		System.out.println("Persona: " + usuario.getName());
		
		if(usuario.getAccount().getRol().getName().equals("ROL_USER")){
			this.tasks = taskService.searchTask(project, usuario, period, taskStatus);
		
		}else{
			this.tasks = taskService.searchTask(project, userPerson, period, taskStatus);
		}
				
		return SUCCESS;
	}
	
	public String searchProfile(){
		Map<String,Object> session = ActionContext.getContext().getSession();
		Long accountId = (Long) session.get("accountId");
		userPerson = userPersonService.searchUserByAccount(accountId);
		
		return SUCCESS;
	}
	
	
	
	public ProjectService getProjectService() {
		return projectService;
	}
	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}
	public TaskService getTaskService() {
		return taskService;
	}
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}
	public UserPerson getUserPerson() {
		return userPerson;
	}
	public void setUserPerson(UserPerson userPerson) {
		this.userPerson = userPerson;
	}
	public Period getPeriod() {
		return period;
	}
	public void setPeriod(Period period) {
		this.period = period;
	}
	public List<Task> getTasks() {
		return tasks;
	}
	public void setTasks(List<Task> tasks) {
		this.tasks = tasks;
	}
	public TaskStatus getTaskStatus() {
		return taskStatus;
	}
	public void setTaskStatus(TaskStatus taskStatus) {
		this.taskStatus = taskStatus;
	}
	public Project getProject() {
		return project;
	}
	public void setProject(Project project) {
		this.project = project;
	}

	public List<UserPerson> getUserPersons() {
		return userPersons;
	}

	public void setUserPersons(List<UserPerson> userPersons) {
		this.userPersons = userPersons;
	}

	public List<Project> getProjects() {
		return projects;
	}

	public void setProjects(List<Project> projects) {
		this.projects = projects;
	}

	public UserPersonService getUserPersonService() {
		return userPersonService;
	}

	public void setUserPersonService(UserPersonService userPersonService) {
		this.userPersonService = userPersonService;
	}

	public List<TaskStatus> getTaskStatus2() {
		return taskStatus2;
	}

	public void setTaskStatus2(List<TaskStatus> taskStatus2) {
		this.taskStatus2 = taskStatus2;
	}

	public Long getIdProject() {
		return idProject;
	}

	public void setIdProject(Long idProject) {
		this.idProject = idProject;
	}
	
}
